package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.PageResult;
import com.itheima.constant.QueryPageBean;
import com.itheima.dao.CheckGroupDao;
import com.itheima.domain.Checkgroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhang_Han
 * @version 1.0
 * @date 2020/9/25 10:21
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    /*添加数据*/
    @Override
    public void add(Checkgroup checkgroup, Integer[] checkitemIds) {
        //编辑checkGroup表;
        checkGroupDao.add(checkgroup);
        //编辑t_checkgroup_checkitem 表;
        //获取添加的checkgroupid,需要使用mybatis提供的last_insert_id()函数来获取id;
        Integer checkgroupId = checkgroup.getId();
        //如果checkitemIds不为null并且长度大于0,就遍历他;
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                //把数据封装到一个map集合中传给dao;
                Map<String, Integer> map = new HashMap<>();
                //给map添加两个id;
                map.put("checkgroupId", checkgroupId);
                map.put("checkitemId", checkitemId);
                //调用方法;
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /*分页条件查询*/
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Checkgroup> page = checkGroupDao.selectByCondition(queryString);
        List<Checkgroup> rows = page.getResult();
        long total = page.getTotal();
        return new PageResult(total, rows);
    }

    /*删除数据*/
    @Override
    public void deleteByid(Integer id) {
        checkGroupDao.deleteByid(id);
    }

    /*根据id查询数据信息,用编辑数据回显*/
    @Override
    public Checkgroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    /*根据检查组id查询所对应的检查项的id*/
    @Override
    public List<Integer> getChencItemIDs(Integer id) {
        return checkGroupDao.getChencItemIDs(id);
    }

    /*编辑检查组信息*/
    @Override
    public void update(Checkgroup checkgroup, Integer[] checkitemIds) {
        //修改检查组的基本信息;
        checkGroupDao.update(checkgroup);
        //清理当前检查组所对应的检查项;操作中间表t_checkgroup_checkitem;
        checkGroupDao.deleteAssocication(checkgroup.getId());
        //重新建立关系;
        Integer checkgroupId = checkgroup.getId();
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroupId", checkgroupId);
                map.put("checkitemId", checkitemId);
                //调用方法;
                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

    /*查询全部数据用于套餐维护的新增页面检查组数据回显*/
    @Override
    public List<Checkgroup> findAll() {
        return checkGroupDao.finAll();
    }
}
